//
//  CalculationPAViewController.m
//  EMMApp
//
//  Created by Mac on 16/7/10.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "CalculationPAViewController.h"
#import "CalculationPAVCell.h"
#import "GetPriceRecordListApi.h"

@interface CalculationPAViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSString *timer;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UILabel *djLabel;

@property (nonatomic, strong) NSMutableArray *dataName1Arr;

@property (nonatomic, strong) NSMutableArray *dataName2Arr;

@property (nonatomic, strong) NSMutableArray *data1Arr;

@property (nonatomic, strong) NSMutableArray *data2Arr;

@end

@implementation CalculationPAViewController

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationController];
    [self creatDropDownView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight-40) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"算价分析";
    [self configLeftBtn];
}

#pragma mark - 生命周期函数调用方法
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    timer = @"1";
    _djLabel.text = @"今天";
    _dataName1Arr = [[NSMutableArray alloc] init];
    _dataName2Arr = [[NSMutableArray alloc] init];
    _data1Arr = [[NSMutableArray alloc] init];
    _data2Arr = [[NSMutableArray alloc] init];
    [self postGetPriceRecordListApiTimeType:@"1"];
}

#pragma mark - Api调用方法
- (void)postGetPriceRecordListApiTimeType:(NSString *)timeType {
    [self showHudInWindow:@"正在加载请稍等..."];

    [_dataName1Arr removeAllObjects];
    [_dataName2Arr removeAllObjects];
    [_data1Arr removeAllObjects];
    [_data2Arr removeAllObjects];
    GetPriceRecordListApi *api = [[GetPriceRecordListApi alloc] initWithWorkloadTimeType:timeType];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetPriceRecordListApi *result = (GetPriceRecordListApi *)request;
        if (result.isSuccess) {
            GetPriceRecordList *get = result.resultData;
            for (HouseTypeList *list in get.houseTypeList) {
                if (list.houseTypeName == nil) {
                    NSLog(@"不存在房源类型名");
                }else{
                [_dataName1Arr addObject:list.houseTypeName];
                [_data1Arr addObject:list.houseTypeNum];
                }
            }
            for (PropertyTypeList *list in get.propertyTypeList) {
                [_dataName2Arr addObject:list.propertyTypeName];
                [_data2Arr addObject:list.propertyTypeNum];
            }

        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [_tableView reloadData];
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

#pragma mark - 跳出层初始化
- (void)creatDropDownView {
    UIView *headBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 40)];
    headBackView.backgroundColor = [UIColor colorFromHexCode:@"#fff5e9"];
    [self.view addSubview:headBackView];
    UIButton *djBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    djBtn.frame = CGRectMake(0, 0, EMM_WIDTH, 40);
    djBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [djBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [djBtn addTarget:self action:@selector(paheadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [headBackView addSubview:djBtn];
    UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 2, 20)];
    la1.backgroundColor = [UIColor whiteColor];
    [headBackView addSubview:la1];
    _djLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
    _djLabel.font = [UIFont systemFontOfSize:15];
    _djLabel.textColor = [UIColor colorFromHexCode:@"#5A5A5A"];
    [headBackView addSubview:_djLabel];
    
    UIImageView *im = [[UIImageView alloc] initWithFrame:CGRectMake(EMM_WIDTH-35, (40-15)/2, 25, 15)];
    im.image = [UIImage imageNamed:@"sanjt_down"];
    [headBackView addSubview:im];
}

- (void)paheadButtonClick:(UIButton *)btn {
    NSArray *arr = [NSArray arrayWithObjects:
                    @"今天",@"昨天",@"本周",
                    @"上周",@"本月",@"上月",@"近三月",@"今年", nil];
    ShowMenuView *menuView = [[ShowMenuView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) title:@"选项" titleNmae:@"选择时间" numLits:nil items:arr];
    menuView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [[UIApplication sharedApplication].keyWindow addSubview:menuView];
    __block ShowMenuView *weakMenuView = menuView;//
    __block CalculationPAViewController *blockSelf = self;
    [menuView setTouchBlock:^(NSString *indet,NSString *number) {
        NSLog(@"%@",indet);
        if ([NSString isStringEmpty2:indet] == NO) {
            _djLabel.text = indet;
            timer = number;
            [blockSelf postGetPriceRecordListApiTimeType:timer];
        }
        [weakMenuView removeFromSuperview];
    }];
}

#pragma mark - UITableView代理
//返回几个表头
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//每一个表头下返回几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 250;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CalculationPAVCellId";
    CalculationPAVCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CalculationPAVCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 249.5, EMM_WIDTH, 0.5)];
    imageView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    if (indexPath.row == 0) {
        [cell addSubview:imageView];
        UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, EMM_WIDTH-20, 20)];
        la1.font = [UIFont systemFontOfSize:15];
        la1.text = @"按户型";
        la1.textColor = [UIColor colorFromHexCode:@"#5A5A5A"];
        [cell addSubview:la1];
    }else {
        UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, EMM_WIDTH-20, 20)];
        la1.font = [UIFont systemFontOfSize:15];
        la1.text = @"按物业类别";
        la1.textColor = [UIColor colorFromHexCode:@"#5A5A5A"];
        [cell addSubview:la1];
    }
    if (indexPath.row == 0) {
        if (_dataName1Arr.count >0) {
            [cell initWithXNumArr:_dataName1Arr oneXNumArr:nil oneNumArr:_data1Arr twoNumArr:nil indexPath:indexPath];
        }else {
            UILabel *jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300/2, EMM_WIDTH, 20)];
            jlLabel.text = @"当前没有记录";
            jlLabel.textAlignment = NSTextAlignmentCenter;
            jlLabel.font = [UIFont systemFontOfSize:15];
            jlLabel.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
            [cell addSubview:jlLabel];
        }
    }else {
        if (_dataName2Arr.count >0) {
            [cell initWithXNumArr:_dataName2Arr oneXNumArr:nil oneNumArr:_data2Arr twoNumArr:nil indexPath:indexPath];
        }else {
            UILabel *jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300/2, EMM_WIDTH, 20)];
            jlLabel.text = @"当前没有记录";
            jlLabel.textAlignment = NSTextAlignmentCenter;
            jlLabel.font = [UIFont systemFontOfSize:15];
            jlLabel.textColor = [UIColor colorFromHexCode:@"#1A1A1A"];
            [cell addSubview:jlLabel];
        }
    }
    return cell;
}

@end
